<?php
require_once("../data/DataHandler.php");
require_once("../config/FileTypes.php");
session_start();

$data_handler = DataHandler::singleton();
//Log out
if (isset($_POST['logout'])) {
  unset($_SESSION['loggedIn']);
}
//If the person has already accessed the site
if (isset($_SESSION['loggedIn'])) {
  //Authentication
  if (isset($_POST['name']) && isset($_POST['password'])) {
    $name = $_POST["name"];
    $password = $_POST["password"];
    if (!($name == "") && $data_handler->authenticate($name, $password)) {
      $_SESSION['loggedIn'] = true;
    } else
      $_SESSION['loggedIn'] = false;
  }
} else
  $_SESSION['loggedIn'] = false;
?>
<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>GARS</title>
    <link rel = "stylesheet" type= "text/css" href = "../styles/basic.css" />
    <link type="text/css" href="../scripts/css/smoothness/jquery-ui-1.8.16.custom.css" rel="Stylesheet" />
    <script src = "../scripts/jquery.js"></script>
    <script src = "../scripts/jquery-validation/jquery.validate.js"></script>
    <script src = "../scripts/js/jquery-ui-1.8.16.custom.min.js"></script>
    <?php
    if ($_SESSION['loggedIn']) {
      $active_user = $_SESSION['active_user'];
      if ($active_user->getRole() == "CHAIR")
        echo "<script src='../scripts/chair.js'></script>";
    }
    ?>
    <script>
      //JQuery
      $(document).ready(function(){
        $("#dialog").dialog({autoOpen:false});
        //form validation
        $("#loginForm").validate();
        //table tabbing
        $('#otsTable').toggle(false);
        $('#viewAY').click(function(){
          $('#ayTable').toggle(true);
          $('#otsTable').toggle(false);
        });
        $('#viewOTS').click(function(){
          $('#ayTable').toggle(false);
          $('#otsTable').toggle(true); 
        });
        //MOVE THIS TO CHAIR.JS
        $('#uploadForm').validate();
        $('#uploadPDF').validate();
        //View single application
        $('tr').dblclick(function() {
          var email = $(this).attr('title');
          $("#dialog").dialog({title:email});
          $("#dialog").dialog('open');
          $.post("viewapp.php", { email: email }, 
            function(data){
              $('#dialog').html(data);
            });
        });
        //Select rows
        $('tr').click(function() {
          var title = $(this).attr('title');
          $('tr[title="' + title+ '"]').toggleClass('selected unselected');
          /*
          if (isSelected()) {
            $('#accept').removeAttr('disabled');
            $('#reject').removeAttr('disabled');
          } else {
            $('#accept').attr('disabled', 'disabled');
            $('#reject').attr('disabled', 'disabled');
          }*/
        });
        
<?php
//Chair functions
if ($_SESSION['loggedIn']) {
  $active_user = $_SESSION['active_user'];
  if ($active_user->getRole() == "CHAIR") {
    $data_handler = DataHandler::singleton();

    //Upload PDF
    if ($emailList = $data_handler->getEmailList()) {
      foreach ($emailList as $email) {
        echo "$('#applicant_email').append('<option>$email</option>');\n";
      }
    } else {
      echo "$('#applicant_email').append('<option>No applications</option>');\n";
      echo "$('#pdfSubmit').attr('disabled','disabled');";
    }

    echo "chair();";
  }
}
?>
          });
          //Checks if any rows are currently selected
          function isSelected() {
            var returnVal = false;
            $('tr').each(function() {
              if ($(this).attr('class') == 'selected') {
                returnVal = true;
              } 
            });
            return returnVal;
          }
    </script>

  </head>
  <body>
    <div class = "wrapper">
      <div id = "dialog"> Application: </div>
      <?php
      //Load logged in page
      if ($_SESSION['loggedIn']) {
        $active_user = $_SESSION['active_user'];
        $name = $active_user->getUsername();
        $role = $active_user->getRole();
        echo "<p>Welcome back, $name!</p>";
        //load role functions
        if ($role == "CHAIR") {
          //chair functions
          require("../includes/chair.html");
        } else if ($role == "STAFF") {
          //staff functions
          require("../includes/staff.html");
        } else if ($role == "FACULTY") {
          //faculty functions
          require("../includes/staff.html");
        }
        //View application table
        if (isset($_POST['viewAppTable'])) {
          echo '<input type = "button" value = "View AY" id = "viewAY" />
                <input type = "button" value = "View OTS" id = "viewOTS" />
               ';
          echo '<div id = "ayTable">';
          try {
            $data_handler->printTable(FileTypes::AY);
          } catch (Exception $e) {
            echo "<label class = 'error'>" . $e->getMessage() . "</label>";
          }
          echo '</div>
                <div id = "otsTable">';
          try {
            $data_handler->printTable(FileTypes::OTS);
          } catch (Exception $e) {
            echo "<label class = 'error'>" . $e->getMessage() . "</label>";
          }
          echo '</div>';
        }
        //Load authentication page
      } else {
        require("../includes/login.html");
      }
      ?>
    </div>
  </body>
</html>